package top.suyarong.ftbqsync;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import top.suyarong.ftbqsync.cmd.FTBQCommands;
import top.suyarong.ftbqsync.listener.FTBQPreventListener;
import top.suyarong.ftbqsync.listener.PlayerDataListener;
import top.suyarong.ftbqsync.timer.DataSaveTask;
import top.suyarong.ftbqsync.timer.DataSyncTask;
import top.suyarong.ftbqsync.utils.*;

import java.util.Objects;

public final class FTBQSync extends JavaPlugin {

    public static FTBQSync instance;

    public static FTBQSync getInstance() {
        return instance;
    }

    private DataSaveTask dataSaveTask;

    private static final TaskPool executor = TaskPool.getInstance();

    private static final LogUtil log = LogUtil.getInstance();

    private DataSyncTask dataSyncTask;

    @Override
    public void onLoad() {
        super.onLoad();
        instance = this;
        dataSyncTask = new DataSyncTask();
        this.saveDefaultConfig();
    }

    @Override
    public void onEnable() {

        // Plugin startup logic
        log.info(this, "FTBQSync ===> enabling");
        Bukkit.getPluginCommand("tnftb").setExecutor(new FTBQCommands());
        Bukkit.getPluginManager().registerEvents(new FTBQPreventListener(), this);
        Bukkit.getPluginManager().registerEvents(new PlayerDataListener(), this);
        NMSUtil.init();
        FTBLibUtil.init();
        FTBQuestUtil.init();
        AstralSorceryUtil.init();
        // 所有插件加载完毕后启动
        this.startSaveTask();
        this.startSyncTask();
        description();
        log.info(this, "&eFTBQSync ===> 已载入成功");
    }

    @Override
    public void onDisable() {

        this.endSyncTask();
        this.endSaveTask();
        log.info(this, "&eFTBQSync ===> 已卸载");
        // Plugin shutdown logic
    }

    private void startSyncTask() {
        if (Objects.isNull(dataSaveTask)) dataSyncTask = new DataSyncTask();
        dataSyncTask.runTaskTimer(this, 0, 1);
        log.info(this, "&e玩家同步tick任务启动");
    }

    private void endSyncTask() {
        if (Objects.isNull(dataSaveTask)) dataSyncTask = new DataSyncTask();
        if (dataSyncTask.isCancelled()) return;
        dataSyncTask.cancel();
        dataSyncTask = null;
        log.info(this, "&e玩家同步tick任务关闭");
    }

    private void startSaveTask() {
        Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
            if (Objects.nonNull(dataSaveTask) && !dataSaveTask.isInterrupted()) {
                dataSaveTask.getTimer().cancel();
                dataSaveTask.interrupt();
                dataSaveTask = null;
            }
            dataSaveTask = new DataSaveTask();
            dataSaveTask.start();
            log.info(this, "&e数据保存定时任务已启动");
        });
    }

    private void endSaveTask() {
        if (Objects.nonNull(dataSaveTask) && !dataSaveTask.isInterrupted()) {
            dataSaveTask.getTimer().cancel();
            dataSaveTask.interrupt();
            dataSaveTask = null;
        }
        try {
            if (executor.shutdown()) log.error(this, "停止数据保存定时任务失败, 部分用户数据可能会回档至上一次插件保存的用户数据");
            log.info(this, "&e数据保存定时任务已关闭");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void description() {
        String version = getDescription().getVersion();

        log.info(this, "版本: {}", version);
        log.info(this, "作者: {}, {}", getDescription().getAuthors(), "QQ:1254806967");
        log.info(this, "  ______ _______ ____   ____   _____ ");
        log.info(this, " |  ____|__   __|  _ \\ / __ \\ / ____| ");
        log.info(this, " | |__     | |  | |_) | |  | | (___  ");
        log.info(this, " |  __|    | |  |  _ <| |  | |\\___ \\| | | | '_ \\ / __|");
        log.info(this, " | |       | |  | |_) | |__| |____) | |_| | | | | (__ ");
        log.info(this, " |_|       |_|  |____/ \\___\\_\\_____/ \\__, |_| |_|\\___|");
        log.info(this, "                                      __/ |           ");
        log.info(this, "                                     |___/                                V{}", version);
    }
}
